<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            margin: 0 auto;
            border-spacing: 0;
        }

        td,
        th {
            border: 1px solid black;
        }
    </style>
</head>

<body>
    <table>
        <tr>
            <th><input type="checkbox" id="all"></th>
            <th>商品</th>
            <th>价钱</th>
        </tr>
        <tbody id="td">
            <tr>
                <td><input type="checkbox"></td>
                <td>iphone</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>ipad Pro</td>
                <td>5000</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>ipad Air</td>
                <td>2000</td>
            </tr>
            <tr>
                <td><input type="checkbox"></td>
                <td>Apple Watch</td>
                <td>2000</td>
            </tr>
        </tbody>
    </table>
    <script>
        //点击全选按钮，下面全选，再点击全选，下面全取消， |没出效果-----下方全选，全选矿被选中，下方少选一个，全选框不被选中
        //https://www.bilibili.com/video/BV1Sy4y1C7ha?p=220&spm_id_from=pageDriver&vd_source=480c28921b1b6c12cb33e4a6f0a94947
        let all = document.getElementById('all');
        let tds = document.getElementById('td').getElementsByTagName('input');
        all.onclick = function () {
            for (let i = 0; i < tds.length; i++) {
                tds[i].checked = this.checked;
            }
        }
        for (let i = 0; i < tds.length; i++) {
            tds[i].onclick = function () {
                let flag = true;
                for (let i = 0; i < tds.length; i++) {
                    if (!tds[i].onclick) {
                        flag = false;
                        break;
                    }
                }
                all.checked = flag;
            }
        }
    </script>

</body>

</html>